/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.asseco.mio;

import com.asseco.mio.codex.Institution;
import com.asseco.persistence.service.HibernateUtil;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

import javax.persistence.Table;
import org.appfuse.model.BaseObject;
import org.appfuse.model.Role;
import org.appfuse.model.User;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.annotations.GenericGenerator;

/**
 *
 * @author gjoko.mitrovski
 */
@Entity
@Table(name = "USER_INSTITUTION")
public class UserInstitution extends BaseObject {
    
    private String id;
    private User user;
    private Institution institution;

    @Id
    @GeneratedValue(generator = "system-uuid")
    @Column(length = 40)
@GenericGenerator(name = "system-uuid", strategy = "uuid")
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }


     @ManyToOne
   @JoinColumn(name = "INSTITUTION")
    public Institution getInstitution() {
        return institution;
    }

    public void setInstitution(Institution institution) {
        this.institution = institution;
    }
 @ManyToOne
   @JoinColumn(name = "app_user")
    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final UserInstitution other = (UserInstitution) obj;
        if ((this.id == null) ? (other.id != null) : !this.id.equals(other.id)) {
            return false;
        }
        if (this.user != other.user && (this.user == null || !this.user.equals(other.user))) {
            return false;
        }
        if (this.institution != other.institution && (this.institution == null || !this.institution.equals(other.institution))) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 61 * hash + (this.id != null ? this.id.hashCode() : 0);
        hash = 61 * hash + (this.user != null ? this.user.hashCode() : 0);
        hash = 61 * hash + (this.institution != null ? this.institution.hashCode() : 0);
        return hash;
    }

    @Override
    public String toString() {
        return "UserInstitution{" + "id=" + id + "user=" + user + "institution=" + institution + '}';
    }



   

    public static List getInstitutionsForUser (User user){

        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
                List resultList = new ArrayList();
               List rolesForUser = user.getRoleList();
               String role;
               Boolean flag = false;
               if (!rolesForUser.isEmpty()){
               for (int i=0; i<rolesForUser.size(); i++){
                   role =  rolesForUser.get(i).toString();
                   if (role.contains("ROLE_ADMIN")) flag = true;
               }
        }
        if ( user.getUsername() == null || flag ){
 Criteria q = session.createCriteria(Institution.class);
             resultList=q.list();
        }else{

      Query q = session.createQuery("from UserInstitution userinst left outer join userinst.institution where userinst.user=" +user.getId());
      List temp = q.list();
      for (int i=0; i<temp.size();i++){
         Object[] tempObj = (Object[]) temp.get(i);
          resultList.add(tempObj[1]);
      }
        }
        return resultList;
    }

    public static List getUsersforInstitution (Institution institution){
        List resultList = new ArrayList();
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
      Query q = session.createQuery("from UserInstitution userinst left outer join userinst.user where userinst.institution=" +institution.getId());
       List temp = q.list();
      for (int i=0; i<temp.size();i++){
         Object[] tempObj = (Object[]) temp.get(i);
          resultList.add(tempObj[1]);
      }
        return resultList;
    }
}
